package com.itheima.controller;

import cn.hutool.core.io.IoUtil;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

//用户信息controller
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    @RequestMapping("/list")
    public List<User> list() throws Exception {
        List<User> userList=userService.findAll();
        //1.加载并读取user.txt
        //InputStream in =new FileInputStream(new File("/Users/Zhuanz/Desktop/javaweb/java/web-ai-project01/springboot-web-01/src/main/resources/user.txt"));
        /*InputStream in=this.getClass().getClassLoader().getResourceAsStream("user.txt");
        ArrayList<String> lines=IoUtil.readLines(in, StandardCharsets.UTF_8,new ArrayList<>());

        //2.解析用户信息，封装user对象-》list集合
        List<User> userList=lines.stream().map(line->{
            String[] parts=line.split(",");
            Integer id=Integer.parseInt(parts[0]);
            String username=parts[1];
            String password=parts[2];
            String name=parts[3];
            Integer age=Integer.parseInt(parts[4]);
            LocalDateTime updateTime=LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return new User(id,username,password,name,age,updateTime);
        }).collect(Collectors.toList());

        //3.返回数据（JSON）
        return userList;*/
        return userList;



    }
}
